package com.wsz.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsz.eduservice.pojo.Chapter;
import com.wsz.eduservice.pojo.Video;
import com.wsz.eduservice.pojo.vo.chapter.ChapterVo;
import com.wsz.eduservice.pojo.vo.chapter.VideoVo;
import com.wsz.eduservice.service.ChapterService;
import com.wsz.eduservice.mapper.ChapterMapper;
import com.wsz.eduservice.service.VideoService;
import com.wsz.servicebase.exceptionhandler.MyTestException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author wangs
* @description 针对表【edu_chapter(课程)】的数据库操作Service实现
* @createDate 2022-09-24 19:05:02
*/
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter>
    implements ChapterService{

    @Autowired
    private VideoService videoService;

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        QueryWrapper<Chapter> queryWrapper = new QueryWrapper();
        queryWrapper.eq("course_id",courseId);
        List<Chapter> chapter = baseMapper.selectList(queryWrapper);

        QueryWrapper<Video> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("course_id",courseId);
        List<Video> video = videoService.list(queryWrapper1);

        List<ChapterVo> finalChapter = new ArrayList<>();

        for (int i = 0; i < chapter.size(); i++) {
            ChapterVo chapterVo = new ChapterVo();
            Chapter chapter1 = chapter.get(i);
            BeanUtils.copyProperties(chapter1,chapterVo);

            List<VideoVo> videos = new ArrayList<>();

            for (int j = 0; j < video.size(); j++) {
                VideoVo videoVo = new VideoVo();
                Video video1 = video.get(j);
                if(chapter1.getId().equals(video1.getChapterId())){
                    BeanUtils.copyProperties(video1,videoVo);
                    videos.add(videoVo);
                }
            }
            chapterVo.setChildren(videos);
            finalChapter.add(chapterVo);
        }

        return finalChapter;
    }

    @Override
    public void removeAllById(String chapterId) {
        videoService.removeByChapterId(chapterId);
        baseMapper.deleteById(chapterId);
    }

    @Override
    public Boolean deleteChapter(String chapterId) {
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        int count = videoService.count(queryWrapper);
        if(count > 0){
            throw new MyTestException(20001,"还有小节不能删除");
        }else{
            int i = baseMapper.deleteById(chapterId);
            return i > 0;
        }
    }

    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<Chapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        baseMapper.delete(queryWrapper);
    }
}




